﻿@model object
@{
    ViewData.TemplateInfo.HtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix.Replace(ViewData.ModelMetadata.PropertyName, "").Trim('.');
    var guid = Guid.NewGuid();
}

<tr id="tr-@guid" class="common-code-mirror">
    <td>
        @Html.TextArea(ViewData.ModelMetadata.PropertyName, Model == null ? "" : Model.ToString(), new { @class = "max" })
    </td>
</tr>
<script type="text/javascript">
    function init() {
        var body = $('#tr-@(guid) textarea').codeMirror({
            sizeProvider: false
        });
        var codeMirrorAPI = body.data("codeMirror");
        function onLoad() {
            window.loading.hide();
        }

        function setTextArea(txt) {
            if (codeMirrorAPI == undefined) {
                return false;
            }
            window.loading.show();
            if (txt && ((!body.val() || !body.val().trim().length))) {
                codeMirrorAPI.setValue(txt);
            } else {
                if (!codeMirrorAPI.getValue()) {
                    codeMirrorAPI.setValue(' ');
                }
                codeMirrorAPI.save();
            }
            codeMirrorAPI.focus();
            codeMirrorAPI.refresh();
            setTimeout(function () { window.loading.hide(); }, 100);
        }

        var fileName = $('#Name');

        var dic = {
            fn: {
                common: function () {
                    fileName.removeAttr('readonly');
                },
                '.rule': function () {
                    fileName.attr({ readonly: true, value: 'Theme' });
                },
                '.js': function () {
                    dic.fn.common();
                    codeMirrorAPI.setOption('mode', 'javascript');
                },
                '.css': function () {
                    dic.fn.common();
                    codeMirrorAPI.setOption('mode', 'css');
                },
                '.txt': function () {
                    dic.fn.common();
                    codeMirrorAPI.setOption('mode', 'text/html');
                },
                '.less': function () {
                    dic.fn.common();
                    codeMirrorAPI.setOption('mode', 'css');
                }
            },
            option: {
                '.js': {

                },
                '.css': {

                },
                '.less': {

                },
                '.txt': {
                    path: '@Html.Raw(Url.Content("~/Areas/Sites/Scripts/codeMirror/")) '
                },
                '.rule': {
                    valueText: '<!--[if lte IE 6]><link rel="stylesheet" type="text/css" href="msie.css" /><![endif]-->\r\n'
                }
            },
            handle: function (extension) {
                var defaultTextOption = this.option[extension];
                if (defaultTextOption != null) {
                    setTextArea(defaultTextOption.valueText);
                }

                onLoad();

                codeMirrorAPI.on('change', function (e) {
                    if (e && e.historySize().undo > 0) {
                        window.leaveConfirm.stop();
                    } else {
                        window.leaveConfirm.pass();
                    }
                });
                if (codeMirrorAPI == undefined) {
                    return false;
                }
                if (dic.fn[extension]!=null) {
                    dic.fn[extension]();
                }                
            }
        };
        var fileExtension = $('#FileExtension');
        if (fileExtension.length) {
            var quickExp = /.js|.css|.txt|.less|.rule/;

            fileExtension.change(function () {
                dic.handle($(this).val());
            });

            //var group = quickExp.exec(fileExtension.val());
            //if (group) {
            dic.handle(fileExtension.val());
            var form = fileExtension.parents('form');
            window.ajaxFormParam = {
                beforeSerialize: function ($form, options) {
                    if (codeMirrorAPI != undefined) {
                        codeMirrorAPI.save();
                    } else {
                        body.val(body.next().text());
                    }
                }
            };
            //}
        }
    };
    $(init);
</script>
